<template>
  <div class="wrap">
    <div class="login-box">
      <div class="login-title">
        <div class="tt">天地云VR管理系统</div>
        <div class="tt-en">TD CLOUD VR MANAGEMENT</div>
      </div>
      <div class="login-content">
        <el-form ref="vFormRef" class="form" :model="vForm">
          <el-form-item
            class="form-group"
            prop="LoginName"
            :rules="{ required: true, message: '账号不能为空' }"
          >
            <el-input v-model="vForm.LoginName" class="form-control" placeholder="账号" clearable />
          </el-form-item>
          <el-form-item
            class="form-group"
            prop="Password"
            :rules="{ required: true, message: '密码不能为空' }"
          >
            <el-input
              v-model="vForm.Password"
              class="form-control"
              placeholder="密码"
              show-password
            />
          </el-form-item>
          <div class="login-btn-box">
            <el-button
              ref="submitBtn"
              class="login-btn"
              :loading="loginLoading"
              type="primary"
              @click="login"
            >
              登录
            </el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { FormInstance } from 'element-plus';
import $user from '@/api/modules/user';
import cookie from '@/api/cookie';
import $router from '@/router';
import { ref, reactive, onBeforeUnmount } from 'vue';

const loginLoading = ref(false);
const vFormRef = ref<FormInstance>();
const vForm = reactive({
  LoginName: '',
  Password: '',
});

const login = async () => {
  loginLoading.value = true;
  try {
    if (!vFormRef.value) return;
    await vFormRef.value.validate();
    const { data } = await $user.login(vForm);
    cookie.setToken(data.data.token);
    $router.replace('/admin');
  } catch (e) {
    console.error(e);
  }
  loginLoading.value = false;
};

const bindDoLogin = (e: Event & { keyCode: number }) => {
  if (e.keyCode === 13) {
    login();
  }
};

document.addEventListener('keydown', bindDoLogin);

onBeforeUnmount(() => {
  document.removeEventListener('keydown', bindDoLogin);
});
</script>

<style scoped lang="scss">
.wrap {
  height: 100vh;
  background: url('../../assets/images/login-bg.png') no-repeat center;
  background-size: cover;
  position: relative;
}

.login-box {
  width: 878px;
  height: 450px;
  border-radius: 6px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-sizing: border-box;
  background: #fff url('../../assets/images/login-box-bg.png') left center no-repeat;
  background-size: auto 100%;
}

.login-title {
  width: 354px;
  position: absolute;
  top: 40px;
  left: 480px;
  text-align: center;

  .tt {
    font-size: 24px;
    font-weight: bold;
  }

  .tt-en {
    font-size: 18px;
    color: #999;
  }
}

.login-content {
  position: absolute;
  top: 150px;
  left: 480px;
  width: 354px;
}

.login-btn-box {
  margin-top: 50px;
}

.login-btn {
  display: block;
  width: 100%;
  font-size: 16px;
  letter-spacing: 6px;
  background: linear-gradient(180deg, rgba(62, 144, 255, 1) 0%, rgba(21, 113, 238, 1) 100%);
  border-radius: 35px;
  border: 0;
  height: 50px;
}

:deep(.el-input__wrapper) {
  padding: 0;
  border-radius: 30px;
  box-shadow: none;
  background: #f6f6f6;
}

:deep(.el-input__inner) {
  border-radius: 30px;
  padding-left: 30px;
  height: 50px;
}

:deep(.el-form-item__error) {
  left: 20px;
}
</style>
